package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysPermission;
import cn.wgx.modules.sys.entity.SysRole;
import cn.wgx.modules.sys.entity.SysRoleExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.dto.DTOSysRole;
import cn.wgx.modules.sys.entity.mapper.SysRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色service
 */

@Service
public class SysRoleService extends BaseService<SysRole> {

    @Resource
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysRoleUserService sysRoleUserService;


    @Transactional(readOnly = true)
    public List<SysRole> getRolesByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRole_idIn(ids);
        List<SysRole> roles = sysRoleMapper.selectByExample(example);
        return roles;
    }

    @Transactional(readOnly = true)
    public List<SysRole> getListByIds(List<Integer> ids) {
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysRoleExample example = new SysRoleExample();
            SysRoleExample.Criteria criteria = example.createCriteria();
            criteria.andRole_idIn(uniList);
            List<SysRole> eduCourseAssignmentHis = sysRoleMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysRole getRolesById(Integer id) {
        SysRole role = sysRoleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Transactional(readOnly = true)
    public List<SysRole> getRoleListBySysUser(SysUser user) {
        if (null != user && StringUtils.isNotBlank(user.getRole_id())) {
//            List<SysRoleUser> listByUserId = sysRoleUserService.getListByUserId(user.getUser_id());
                SysRoleExample example = new SysRoleExample();
                SysRoleExample.Criteria criteria = example.createCriteria();
                String[] split = user.getRole_id().split(",");//多角色查询 1，2，3
//                String[] split = new String[listByUserId.size()];
//                for(int i=0,n=listByUserId.size();i<n;i++){
//                    split[i] = listByUserId.get(i).getRole_id()+"";
//                }
                if (split.length > 0) {
                    StringBuffer sb = new StringBuffer("t.role_id in( ");
                    for (String s : split) {
                        sb.append(s);
                        sb.append(",");
                    }
                    sb.append("-1) and 1=");
                    addCustomSQL(criteria, sb.toString(), "1");
                    return sysRoleMapper.selectByExample(example);
                }
        }
        return emptyList;
    }

    @Transactional(readOnly = true)
    public List<SysRole> getList(SysRole record, String title, String order) {
        SysRoleExample example = new SysRoleExample();
        if (null != record) {
            SysRoleExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(record.getRoleKey())) {
                criteria.andRoleKeyEqualTo(record.getRoleKey());
            } else if (StringUtils.isNotEmpty(record.getRoleName())) {
                if ("-1".equals(title)) {
                    criteria.andRoleNameEqualTo(record.getRoleName());  //  -1时精确查询
                } else {
                    criteria.andRoleNameLike("%" + record.getRoleName() + "%");
                }
            }
        }
        if ("desc".equals(order)) {
            example.setOrderByClause(title + " desc");
        } else if (null != order) {
            example.setOrderByClause(title + " asc");
        }
        return sysRoleMapper.selectByExample(example);
    }

    /**
     * 获取单个角色详细信息
     *
     * @param role_id
     * @return
     */
    @Transactional(readOnly = true)
    public SysRole getOne(Integer role_id) {
        SysRole roles = sysRoleMapper.selectByPrimaryKey(role_id);
        DTOSysRole dtoSysRole = turnData(roles);
        return dtoSysRole;
    }

    /**
     * 转dto
     *
     * @param sysRole
     * @return
     */
    public DTOSysRole turnData(SysRole sysRole) {
        DTOSysRole dtoSysRole = new DTOSysRole();
        BeanUtils.copyProperties(sysRole, dtoSysRole);
        List<SysPermission> sysPermissions = sysPermissionService.getPermissionsByRoleId(sysRole.getRole_id());
        dtoSysRole.set_sysPermissions(sysPermissions);
        return dtoSysRole;
    }

    /**
     * 转dtos
     *
     * @param sysRoles
     * @return
     */
    public List<DTOSysRole> turnData(List<SysRole> sysRoles) {
        List<DTOSysRole> list = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            DTOSysRole dtoSysRole = turnData(sysRole);
            list.add(dtoSysRole);
        }
        return list;
    }

    @Transactional
    public SysRole insertOne(SysRole record) {
        sysRoleMapper.insertSelective(record);
        return record;
    }


    @Transactional
    public int updateOne(SysRole record) {
        return sysRoleMapper.updateByPrimaryKeySelective(record);
    }


    @Transactional
    public int deleteOne(Integer id) {
        return sysRoleMapper.deleteByPrimaryKey(id);
    }


}